package cn.stylefeng.guns.modular.business.frequency.financecheck.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.enums.StatusExceptionEnum;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.business.frequency.financecheck.entity.FinanceCheck;
import cn.stylefeng.guns.modular.business.frequency.financecheck.mapper.FinanceCheckMapper;
import cn.stylefeng.guns.modular.business.frequency.financecheck.model.param.FinanceCheckParam;
import cn.stylefeng.guns.modular.business.frequency.financecheck.service.FinanceCheckService;
import cn.stylefeng.guns.modular.business.frequency.financecheck.enums.FinanceCheckExceptionEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 对账单结算记录表 服务实现类
 *
 * @author yujie
 * @date 2023/05/09 17:17
 */
@Service
public class FinanceCheckServiceImpl extends ServiceImpl<FinanceCheckMapper, FinanceCheck> implements FinanceCheckService {

    @Override
    public PageResult<FinanceCheck> page(FinanceCheckParam financeCheckParam) {

        // 构造条件
        LambdaQueryWrapper<FinanceCheck> queryWrapper = new LambdaQueryWrapper<>();


        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<FinanceCheck> list(FinanceCheckParam financeCheckParam) {

        // 构造条件
        LambdaQueryWrapper<FinanceCheck> queryWrapper = new LambdaQueryWrapper<>();


        return this.list(queryWrapper);
    }

    @Override
    public void add(FinanceCheckParam financeCheckParam) {

        // 将dto转为实体
        FinanceCheck financeCheck = new FinanceCheck();
        BeanUtil.copyProperties(financeCheckParam, financeCheck);


        this.save(financeCheck);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(FinanceCheckParam financeCheckParam) {

        // 根据id查询实体
        FinanceCheck financeCheck = this.queryFinanceCheck(financeCheckParam);


        // 更新实体
        this.updateById(financeCheck);

    }

    @Override
    public void edit(FinanceCheckParam financeCheckParam) {

        // 根据id查询实体
        FinanceCheck financeCheck = this.queryFinanceCheck(financeCheckParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(financeCheckParam, financeCheck);

        // 不能修改状态，用修改状态接口修改状态
        financeCheck.setStatus(null);

        this.updateById(financeCheck);
    }

    @Override
    public FinanceCheck detail(FinanceCheckParam financeCheckParam) {
        return this.queryFinanceCheck(financeCheckParam);
    }

    @Override
    public void changeStatus(FinanceCheckParam financeCheckParam) {
        Long id = financeCheckParam.getId();
        Integer status = financeCheckParam.getStatus();

        // 更新枚举，更新只能更新未删除状态的
        LambdaUpdateWrapper<FinanceCheck> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
            .eq(FinanceCheck::getId, id)
            .set(FinanceCheck::getStatus, status);
        boolean update = this.update(updateWrapper);
        if (!update) {
            throw new ServiceException(StatusExceptionEnum.UPDATE_STATUS_ERROR);
        }
    }


    /**
     * 获取对账单结算记录表
     *
     * @author yujie
     * @since 2023/05/09 17:17
     */
    private FinanceCheck queryFinanceCheck(FinanceCheckParam financeCheckParam) {
        FinanceCheck financeCheck = this.getById(financeCheckParam.getId());
        if (ObjectUtil.isEmpty(financeCheck)) {
            throw new ServiceException(FinanceCheckExceptionEnum.NOT_EXISTED);
        }
        return financeCheck;
    }
}